package com.google.code.mochaccino.framework.ui.demo.pages;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.freemarker.TemplateManager;
import com.google.code.mochaccino.framework.rest.Root;
import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.ui.demo.SubjectUtil;
import com.google.code.mochaccino.framework.ui.freemarker.TemplateOutput;
import com.google.code.mochaccino.framework.ui.i18n.ResourceBundleUtil;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.CollectionModel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;


/**
 *
 */
@Path( Frame.PATH )
public class Frame {

	/** The Frame Service */
	private FrameService service;

	/** Template Manager */
	private TemplateManager manager;

	public static final  String PATH       = "/frame";
	public static final String PRIV_FRAME = "Frame";

	@Inject
	public Frame( TemplateManager manager, FrameService service ) {
		this.manager = manager;
		this.service = service;
	}

	/** Login CSS */
	@GET
	@Produces( MimeTypes.TYPE_CSS )
	@RequiresPermissions( PRIV_FRAME )

	public Response frame_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/frame/frame.less.ftl" ) ).build();
	}

	@GET
	@Produces( MimeTypes.TYPE_HTML )
	@RequiresPermissions( PRIV_FRAME )

	public Response frame_html( @QueryParam( "lang" ) String lang ) {

		Subject subject = SecurityUtils.getSubject();
		Object credential = subject.getPrincipal();
		Locale loc = SubjectUtil.setLocale( lang );
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "nodes", new CollectionModel( service.getTreeNodes( credential ), BeansWrapper.getDefaultInstance() ) );
		parameters.put( "username", service.getFullName( credential ) );
		parameters.put( "environment", service.getEnvironment() );
		parameters.put( "timestamp", new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" ).format( new Date() ) );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/frame/frame", loc ) );
		parameters.put( "i18n_header", ResourceBundleUtil.getResourceBundleModel( "web/pages/frame/header", loc ) );
		parameters.put( "urlLogout", Root.PATH_LOGOUT );
		parameters.put( "urlSettings", service.getSettingsUrl( credential ) );
		parameters.put( "urlHelp", service.getHelpUrl() );
		parameters.put( "hideTree", Boolean.FALSE );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/frame/frame.html.ftl", parameters ) ).build();
	}
}
